package com.jemmyee.core.web.listener;

import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

/**
 * @Description: 系统全局设置
 * @author:jemmyee@gmail.com
 * @date:2010-11-15
 * @version:v1.0
 */
public class ApplicationListener implements ServletContextListener {
	public static Service srv;

	public void contextDestroyed(ServletContextEvent arg0) {
//		closeService();
	}

	public void contextInitialized(ServletContextEvent event) {
		ServletContext application = event.getServletContext();
		String projectPath = application.getRealPath(File.separator);
		// String
		// mode=ConfigXmlUtils.getConfig(projectPath+Constants.LANGUAGE_XML_CONFIG_NAME,
		// Constants.CONFIG_LANGUAGE_DEFAULT_NAME);
		// 全局路径
		application.setAttribute("approot", application.getContextPath());
		// //加载语言
		// WebUtils.initViewParam(application,mode);

//		try {
//			OutboundNotification outboundNotification = new OutboundNotification();
//			srv = new Service();
//			SerialModemGateway gateway = new SerialModemGateway("modem.com1","COM3", 9600, "wavecom", "9600");
//			gateway.setInbound(true);
//			gateway.setOutbound(true);
//			gateway.setSimPin("0000");
//			gateway.setOutboundNotification(outboundNotification);
//			srv.addGateway(gateway);
//
//			srv.startService();
//		} catch (Exception e) {
//			closeService();
//		}
	}

	public class OutboundNotification implements IOutboundMessageNotification {
		public void process(String gatewayId, OutboundMessage msg) {
			System.out.println("Outbound handler called from Gateway: "	+ gatewayId);
			System.out.println(msg);
		}
	}

	private void closeService() {
		try {
			srv.stopService();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
